package com.ys.data.api.async;

import com.common.annotation.ApiLogDetail;
import com.common.base.BaseEnum;
import com.common.pojo.Result;
import com.common.util.RedisUtil;
import com.common.vo.DictDataVo;
import com.common.vo.RankingListVo;
import com.common.vo.RoleVo;
import com.xxl.job.core.handler.annotation.XxlJob;
import com.ys.data.client.AuthClient;
import com.ys.data.client.CoreClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;


/**
 * @version: V1.0
 * @author: szw
 * @className: AsyncService
 * @packageName: AsyncService
 * @description: 异步处理任务接口方便统一调用
 * @date: 2021-1-21
 **/
@Component
@Slf4j
public class AsyncService {

    @Resource
    private AuthClient authClient;
    @Resource
    private RedisUtil redisUtil;
    @Resource
    private CoreClient coreClient;
    /**
     * 同步角色数据到redis
     * @return
     */
    @Async("asyncServiceExecutor")
    @XxlJob("initRoleDataJobHandler")
    @ApiLogDetail(detail = "API服务-同步数据-同步角色数据到redis",operationType=BaseEnum.SYNC)
    public Integer initRoleDataResult() {
        log.info("开始异步处理角色数据信息");
        RoleVo roleVo = RoleVo.builder().enable(1).build();
        Result<?> result = authClient.findRoles(roleVo);
        log.info("异步处理角色数据信息完毕,本次查询到" + result.getData());
        String key = BaseEnum.INIT_ROLE_REDIS_KEY.getCode();
        return redisUtil.loadDataRedis(result,key,99999);
    }

    /**
     * 同步字典数据到redis
     * @return
     */
    @Async("asyncServiceExecutor")
    @XxlJob("initDictDataJobHandler")
    @ApiLogDetail(detail = "API服务-同步数据-同步字典数据到redis",operationType=BaseEnum.SYNC)
    public Integer initDictData() {
        log.info("开始异步初始化字典");
        DictDataVo dictDataVo = DictDataVo.builder().enableFlag(1).build();
        Result<?> result = coreClient.findDictDataList(dictDataVo);
        log.info("异步处理角色数据信息完毕,本次查询到" + result.getData());
        String key = BaseEnum.INIT_DICT_REDIS_KEY.getCode();
        return redisUtil.loadDataRedis(result,key,99999);
    }

    /**
     * 同步排行榜数据到Redis
     * @return
     */
    @Async("asyncServiceExecutor")
    @XxlJob("leaderboardJobHandler")
    @ApiLogDetail(detail = "API服务-同步数据-同步排行榜数据到Redis",operationType=BaseEnum.SYNC)
    public Integer leaderboard() {
        log.info("开始异步初始化排行榜数据");
        Result<?> result = coreClient.rankingListPage(new RankingListVo(),0,100);
        log.info("异步加载排行榜数据信息完毕,本次查询到" + result.getData());
        String key = BaseEnum.INIT_DICT_LEADERBOARD_KEY.getCode();
        return redisUtil.loadDataRedis(result,key,99999);
    }
}
